# -*- coding: utf-8 -*-
import tornado
import app.tools.SafeMonitorTools.api as api
from app.views import SmBaseHandler
import app.tools.SafeMonitorTools.m_position_layer_type as smType
mine_id = smType.default_MineId
class zTreeAdminHandler(SmBaseHandler):
    @tornado.gen.coroutine
    def get(self):
        servicetype = self.get_query_argument('servicetype')
        type = self.get_query_argument('type')
        responses = []
        if type == 'staff':
            if servicetype == 'admin':
                rows = yield api.query_staff_admin(mine_id)
                print(rows)
                for row in rows:
                    staff = {}
                    staff['mine_id'] = row[0]
                    staff['node_id'] = row[2]
                    staff['is_parent'] = False
                    staff['parent_id'] = row[3]
                    staff['node_type'] = row[4]
                    staff['region'] = row[5]
                    staff['place'] = row[6]
                    staff['name'] = " - ".join([staff['node_id'],staff['place'],staff['node_type']])
                    responses.append(staff)
        elif type == 'broadcast':
            if servicetype == 'admin':
                rows = yield api.query_broadcast_admin(mine_id)
                for row in rows:
                    broad = {}
                    broad['mine_id'] = row[0]
                    broad['node_id'] = row[1]
                    broad['is_parent'] = False
                    broad['parent_id'] = row[2]
                    broad['node_type'] = row[3]
                    broad['place'] = row[4]
                    broad['name'] = " - ".join([broad['node_id'],broad['place'],broad['node_type']])
                    responses.append(broad)
        elif type == 'gas':
            if servicetype == 'admin':
                rows = yield api.query_gas_admin(mine_id)
                for row in rows:
                    gas = {}
                    gas['mine_id'] = row[0]
                    gas['node_id'] = row[1]
                    # gas['is_parent'] = False
                    gas['parent_id'] = row[2]
                    gas['node_type'] = row[3]
                    gas['place'] = row[8]
                    gas['name'] = " - ".join([str(gas['node_id']),str(gas['place']),str(gas['node_type'])])
                    responses.append(gas)
        elif type == 'video':
            if servicetype == 'admin':
                rows = yield api.query_video_admin(mine_id)
                for row in rows:
                    video = {}
                    video['node_id'] = row[1]
                    video['is_parent'] = False
                    video['parent_id'] = row[2]
                    video['node_type'] = row[3]
                    video['place'] = row[4]
                    video['deviceId'] = row[5]
                    video['manufacture'] = row[6]
                    video['ip'] = row[7]
                    video['port'] = row[8]
                    video['username'] = row[9]
                    video['password'] = row[10]
                    video['codec'] = row[11]
                    video['channel'] = row[12]
                    video['url'] = row[13]
                    video['create_time'] = row[14].timestamp()
                    video['name'] = " - ".join([video['place'],video['node_type']])
                    responses.append(video)
        self.finish({'data':responses})

